const Joi = require('joi');
const validations = require('./validations');

function createJoiValidationError(message, path = ['value']) {
	const schema = Joi.string().required(); // 定义一个简单的 schema
	const {
		error
	} = schema.validate(undefined, {
		abortEarly: false
	}); // 故意验证错误
	if (error) {
		// 修改生成的错误以符合你的需求
		error.details = [{
			message,
			path,
			type: 'custom.error',
			context: {
				label: path.join('.') || 'value'
			},
		}, ];
		error.message = message; // 覆盖 message
	}
	return error;
}




// 手动创建一个 ValidationError 实例
exports.createErrormessage = createJoiValidationError;
exports.validations = validations;
exports.validate = function(schema, params) {
	console.log("我进来校验", params);
	console.log("我的验证规则", schema);
	if (!params) throw createJoiValidationError('参数异常');
	const {
		value,
		error
	} = Joi.compile(schema).validate(params);

	if (error) throw error;

	return value;
};